/*
*******************************************************************************
** Copyright (C), 2014-2020, TONLY AV R&D CENTER SOFTWARE DEVELOPMENT DEPARTMENT
** FileName: 
** <author>:             
** Date:
** Description:
**
** Others:
** Function List:
**   1.
** History:
**  <Author>    <date>    <content>    <version>
*******************************************************************************
*/
//*****************************************************************************
// File Include
//*****************************************************************************
#include "cdn_light.h"
#include "app_log.h"
#include "sl_status.h"
#include "sl_btmesh_api.h"
#include "input_mesh.h"
#include "cdn_nvm3.h"
#include "cdn_pwm.h"
#include "cdn_adv.h"
#include "cdn_key.h"
#include "app.h"
#include "sl_simple_timer.h"
#include "app_assert.h"
#include "sl_status.h"

//*****************************************************************************
// Local Data
//*****************************************************************************
static uint8_t provision_mode = 0;  //配网标志，已经入网：1，没入网：0
static volatile uint8_t EnterGroupFailFlag = 0;
static LightCurrentState g_CDN_LedState = {0};
LightInfo CdnLightNode = {0};

static Power_SelectModeInfo Power_SelectMode = {0};
static Power_SelectInfo PowerSelect = {0};

static uint16_t LightCurrentValue = 0;

static uint8_t KeyDimmingUpDown = 0;
static uint8_t KeyCTUpDowm = 0;
static uint8_t FactoryTimerFlag = 0;

static uint8_t ConfigBlinkFlag = 0;
static CdnWorkMode CurrentWorkMode = CDN_WORK_MODE;   //灯的工作模式 0:工作模式 1:厂测模式
static SceneGroup SceneArray[SCENE_CNT] = {0};   //场景信息
static uint8_t FlashSaveFlag = 0;  //保存flash标志
//*****************************************************************************
// Global Data
//*****************************************************************************
LIGHT_EFFECT_CMD LightEffectCMD;
LightCurrentState *P_CdnLightNode = &g_CDN_LedState;
uint8_t LightnessAndTempSyncChange = 0;    
bd_addr node_mac_adr;

extern const uint8_t *__mesh_dcd_ptr;

static uint8_t GroupOnoffJudgeFlag = 0;

//设置组开关指令判断标志
void SetGroupOnoffJudgeFlag(uint8_t Flag)
{
  GroupOnoffJudgeFlag = Flag;
}

//获取组开关指令判断标志
uint8_t GetGroupOnoffJudgeFlag(void)
{
  return GroupOnoffJudgeFlag;
}

//*****************************************************************************
// API Functions
//*****************************************************************************
//获取主元素地址
uint16_t cdn_get_element_address(void)
{
  uint16_t element_addr = 0;
  sl_btmesh_node_get_element_address(0,&element_addr);
  return element_addr;
}
uint16_t cdn_get_product_id(void)
{
#if CDN_LIGHT_BULB
#if CDN_MAX_POWER_12W_24W  //24W驱动
  if(CDN_CCT_LIGHT == GetCdnLightDeviceType())
    return 0x810A;
  else
    return 0x8109;
#else                      //12W驱动
  if(CDN_CCT_LIGHT == GetCdnLightDeviceType())
    return 0x8108;
  else
    return 0x8107;
#endif
#endif

#if CDN_LIGHT_STRIP
  if(0 == CDN_STRIP_DIMMING_LIGHT)//恒压灯带
    return 0x810C;
  else if(1 == CDN_STRIP_DIMMING_LIGHT)
    return 0x810B;
#endif

}
//获取当前节点Mac地址
void cdn_get_node_information(void)
{
	uint8_t mac_addr_type = 0;
#if CDN_DEBUG_TRACE
	uint16_t software_ver;
	uint16_t element_adr;
	uint16_t pid;
#endif
	
	sl_status_t get_identity_address_sc = sl_bt_system_get_identity_address(&node_mac_adr, &mac_addr_type);
  if(get_identity_address_sc == SL_STATUS_OK)
  {
    TRACE("zx: cdn_get_node_mac_address = %02x %02x %02x %02x %02x %02x\n",node_mac_adr.addr[0],node_mac_adr.addr[1],node_mac_adr.addr[2],node_mac_adr.addr[3],node_mac_adr.addr[4],node_mac_adr.addr[5]);
  }
  else
  {
    TRACE("zx: cdn_get_node_mac_address Fail = 0x%lx \r\n",get_identity_address_sc);
  }
#if CDN_DEBUG_TRACE
	software_ver = (*(__mesh_dcd_ptr + 5) << 8) | *(__mesh_dcd_ptr + 4);
	TRACE("zx: current software version : 0x%04x\n",software_ver);

	element_adr = cdn_get_element_address();
	TRACE("zx: node element address : 0x%04x\n",element_adr);

	pid = cdn_get_product_id();
	TRACE("zx: current product id : 0x%04x\n",pid);
#endif
  if(GetProvisionFlag())
    {
      TRACE("lj:  provisioned = 0x%02x,CdnLightNode.group_adr = 0x%04x\n",GetProvisionFlag(),CdnLightNode.group_adr);
    }
}

//获取当前节点主元素的序列号与网络中的IV index
void cdn_get_node_primary_element_seqnum(void)
{
	uint32_t seqnum = 0;
	uint32_t seqremain = 0;
	uint32_t ivindex = 0;
	uint8_t ivstate = 0;
	
	sl_status_t sc;
	sc = sl_btmesh_node_get_element_seqnum(0, &seqnum);
	sc = sl_btmesh_node_get_seq_remaining(0, &seqremain);

	if(sc == SL_STATUS_OK)
	{
		TRACE("zx: cdn_get_node_primary_element_seqnum 0x%lx,seqnum_remain 0x%lx\n",seqnum,seqremain);
	}

	sc = sl_btmesh_node_get_ivupdate_state(&ivindex, &ivstate);
	if(sc == SL_STATUS_OK)
	{
		TRACE("zx: current iv index = 0x%lx,iv state = 0x%x\n",ivindex,ivstate);
	}
}

//设置当前灯功率模式档位,若档位有改变，则存入Flash
void SetCurrentLightPowerWorkMode(uint8_t PowerMode)
{
  if(Power_SelectMode.Power_Mode != PowerMode)
    {
      Power_SelectMode.Power_Mode = PowerMode;
      cdn_nvm3_delete(POWER_MODE_KEY_ID60);
      cdn_nvm3_write(POWER_MODE_KEY_ID60,(uint8_t *)(&Power_SelectMode),sizeof(Power_SelectMode));
    }
  TRACE("lj: LightPowerWorkMode = %x\r\n",GetCurrentLightPowerWorkMode());
}

//获取当前灯功率模式档位
uint8_t GetCurrentLightPowerWorkMode(void)
{
  return Power_SelectMode.Power_Mode;
}

//设置当前灯功率模式对应的实测值
void SetCurrentLightPowerWorkModeValue(CdnPowerValue Value)
{
  PowerSelect.Power_Value = Value;
  if(GetCurrentLightPowerWorkModeValue()!= GetCurrentLightPrePowerWorkModeValue())
    {
      TRACE("lj: SetCurrentLightPowerWorkMode:%d\n",GetCurrentLightPowerWorkModeValue());
      //SetCurrentLightPrePowerWorkModeValue(GetCurrentLightPowerWorkModeValue());
    }
}
//获取当前灯功率对应的实测值
CdnPowerValue GetCurrentLightPowerWorkModeValue(void)
{
  return PowerSelect.Power_Value;
}

//设置上一次灯功率模式对应的实测值
void SetCurrentLightPrePowerWorkModeValue(CdnPowerValue Value)
{
  PowerSelect.PrePower_Value = Value;
}
//获取上一次当前灯功率对应的实测值
CdnPowerValue GetCurrentLightPrePowerWorkModeValue(void)
{
  return PowerSelect.PrePower_Value;
}

//设置当前灯电流值
void SetLightCurrentValue(uint16_t Value)
{
  LightCurrentValue = Value;
}
//获取当前灯电流值
uint16_t GetLightCurrentValue(void)
{
  return LightCurrentValue;
}


void LightPowerWorkModeInit(void)
{
#if CDN_MAX_POWER_12W_24W
  SetLightCurrentValue(CDN_24W_Min_Cur);//电流值默认设为最小
#else
  SetLightCurrentValue(CDN_12W_Min_Cur);//电流值默认设为最小
#endif
  switch(GetCurrentLightPowerWorkMode())
  {
    case Power_5W_Mode:
      SetCurrentLightPowerWorkModeValue(Power_5W);
      break;
    case Power_7W_Mode:
      SetCurrentLightPowerWorkModeValue(Power_7W);
      break;
    case Power_9W_Mode:
      SetCurrentLightPowerWorkModeValue(Power_9W);
      break;
    case Power_12W_Mode:
      SetCurrentLightPowerWorkModeValue(Power_12W);
      break;
    case Power_15W_Mode:
      SetCurrentLightPowerWorkModeValue(Power_15W);
      break;
    case Power_18W_Mode:
      SetCurrentLightPowerWorkModeValue(Power_18W);
      break;
    case Power_20W_Mode:
      SetCurrentLightPowerWorkModeValue(Power_20W);
      break;
    case Power_24W_Mode:
      SetCurrentLightPowerWorkModeValue(Power_24W);
      break;
    default:
#if CDN_MAX_POWER_12W_24W
      SetCurrentLightPowerWorkModeValue(Power_15W);
#else
      SetCurrentLightPowerWorkModeValue(Power_5W);
#endif
      break;
  }
  TRACE("lj: LightPowerWorkModeValue = %d\r\n",GetCurrentLightPowerWorkModeValue());
}


//APP设置某组灯的功率档位值
void SetCurrentGroupLightnessPowerValue(uint16_t GroupAdr,uint8_t PowerMode)
{
  if(ERROR == is_valid_group(GroupAdr))
  {
    return;
  }
  if(CdnLightNode.group_adr == GroupAdr)
  {
      SetCurrentLightPowerWorkMode(PowerMode);
      switch(GetCurrentLightPowerWorkMode())
      {
#if CDN_MAX_POWER_12W_24W
        case Power_default_Mode:
          SetCurrentLightPowerWorkModeValue(Power_15W);
          break;
        case Power_15W_Mode:
          SetCurrentLightPowerWorkModeValue(Power_15W);
          break;
        case Power_18W_Mode:
          SetCurrentLightPowerWorkModeValue(Power_18W);
          break;
        case Power_20W_Mode:
          SetCurrentLightPowerWorkModeValue(Power_20W);
          break;
        case Power_24W_Mode:
          SetCurrentLightPowerWorkModeValue(Power_24W);
          break;
#else
        case Power_default_Mode:
          SetCurrentLightPowerWorkModeValue(Power_5W);
          break;
        case Power_5W_Mode:
          SetCurrentLightPowerWorkModeValue(Power_5W);
          break;
        case Power_7W_Mode:
          SetCurrentLightPowerWorkModeValue(Power_7W);
          break;
        case Power_9W_Mode:
          SetCurrentLightPowerWorkModeValue(Power_9W);
          break;
        case Power_12W_Mode:
          SetCurrentLightPowerWorkModeValue(Power_12W);
          break;
#endif
        default:
          SetCurrentLightPowerWorkMode(Power_default_Mode);
#if CDN_MAX_POWER_12W_24W
          SetCurrentLightPowerWorkModeValue(Power_15W);
#else
          SetCurrentLightPowerWorkModeValue(Power_5W);
#endif
          break;
      }
      TRACE("lj: APPTX_CurrentLightPowerWorkMode:%d\n",GetCurrentLightPowerWorkModeValue());
  }
}


/****************************/

//设置当前灯工作模式
void SetCurrentLightWorkMode(CdnWorkMode Mode)
{
	CurrentWorkMode = Mode;
}
//获取当前灯工作模式
CdnWorkMode GetCurrentLightWorkMode(void)
{
	return CurrentWorkMode;
}
//设置当前亮度值
void SetCurrentBrightness(uint8_t CurrentBrightness)
{
  if(CurrentBrightness > 100)
  {
    CurrentBrightness = 100;
  }
  CdnLightNode.CurrentGroup.Lightness = CurrentBrightness;
}
//获取当前亮度值
uint8_t GetCurrentBrightness(void)
{
  if(CdnLightNode.CurrentGroup.Lightness > 100)
  {
    CdnLightNode.CurrentGroup.Lightness = 100;
  }
  return CdnLightNode.CurrentGroup.Lightness;
}
//设置目标亮度值
void SetTargetBrightness(uint8_t TargetBrightness)
{
  if(TargetBrightness > 100)
  {
    TargetBrightness = 100;
  }
  g_CDN_LedState.TargetLightnes = TargetBrightness;
}
//获取目标亮度值
uint8_t GetTargetBrightness(void)
{
  if(g_CDN_LedState.TargetLightnes > 100)
  {
    g_CDN_LedState.TargetLightnes = 100;
  }
  return g_CDN_LedState.TargetLightnes;
}
//设置上一次目标亮度值
void SetPreTargetBrightness(uint8_t TargetBrightness)
{
	if(TargetBrightness > 100)
	{
		TargetBrightness = 100;
	}
	g_CDN_LedState.PreTargetLightness = TargetBrightness;
	TRACE("zx: SetPreTargetBrightness:%d\n",g_CDN_LedState.PreTargetLightness);
}
//获取上一次目标亮度值
uint8_t GetPreTargetBrightness(void)
{
	if(g_CDN_LedState.PreTargetLightness > 100)
  {
    g_CDN_LedState.PreTargetLightness = 100;
  }
	TRACE("zx: GetPreTargetBrightness:%d\n",g_CDN_LedState.PreTargetLightness);
  return g_CDN_LedState.PreTargetLightness;
}
//设置当前色温值
void SetCurrentCTvalue(uint8_t CT)
{
  if(CT > 100)
  {
    CT = 100;
  }
  CdnLightNode.CurrentGroup.CT = CT;
}
//获取当前色温值
uint8_t GetCurrentCTvalue(void)
{
  if(CdnLightNode.CurrentGroup.CT > 100)
  {
    CdnLightNode.CurrentGroup.CT = 100;
  }
  return CdnLightNode.CurrentGroup.CT;
}
//设置目标色温值
void SetTargetCTvalue(uint8_t TargetCT)
{
  if(TargetCT > 100)
  {
    TargetCT = 100;
  }
  g_CDN_LedState.TargetCT = TargetCT;
}
//获取目标色温值
uint8_t GetTargetCTvalue(void)
{
  if(g_CDN_LedState.TargetCT > 100)
  {
    g_CDN_LedState.TargetCT = 100;
  }
  return g_CDN_LedState.TargetCT;
}
//计算亮度渐变时间(ms)
uint16_t ChangeToTargetBrightnessRemainTime(void)
{
	//设置起始index
  g_CDN_LedState.DimIndex = GetCurrentBrightness() * (DIM_MAX_VALUE_INDEX / PWM_MAX_DUTY_CYCLE) + P_CdnLightNode->DimEndIndex;

  //渐变亮
  if(GetTargetBrightness() > GetCurrentBrightness())
  {
    g_CDN_LedState.DimFadeFlag = 1;

    if(LightnessAndTempSyncChange == 1)
    {
      return ((GetTargetBrightness() - GetCurrentBrightness()) * LIGHT_REMAIN_INTERVAL_HALF + 100);
    }
    else
    {
        if(GetGroupOnoffJudgeFlag())
          {
            return ((GetTargetBrightness() - GetCurrentBrightness()) * LIGHT_REMAIN_INTERVAL_HALF + 100);
          }
        else
          {
            return ((GetTargetBrightness() - GetCurrentBrightness()) * LIGHT_REMAIN_INTERVAL + 100);
          }
    }
  }
  //渐变灭
  else if(GetTargetBrightness() < GetCurrentBrightness())
  {
    g_CDN_LedState.DimFadeFlag = 0;

    if(LightnessAndTempSyncChange == 1)
    {
      return ((GetCurrentBrightness() - GetTargetBrightness()) * LIGHT_REMAIN_INTERVAL_HALF + 100);
    }
    else
    {
        if(GetGroupOnoffJudgeFlag())
          {
            return ((GetCurrentBrightness() - GetTargetBrightness()) * LIGHT_REMAIN_INTERVAL_HALF + 100);
          }
        else
          {
            return ((GetCurrentBrightness() - GetTargetBrightness()) * LIGHT_REMAIN_INTERVAL + 100);
          }
    }
  }
  else
  {
    g_CDN_LedState.DimFadeFlag = -1;
    TRACE("zx: ChangeToTargetBrightnessRemainTime Brightness no change\n");
    return 0;
  }
}
//设置亮度渐变时间(ms)
void SetBrightnessRemainTime(uint16_t time)
{
  g_CDN_LedState.DimRemainTime = time;
}
//获取亮度渐变时间(ms)
uint16_t GetBrightnessRemainTime(void)
{
  return g_CDN_LedState.DimRemainTime;
}
//计算色温渐变时间(ms)
uint16_t ChangeToTargetCTRemainTime(void)
{
  //设置起始index
  g_CDN_LedState.CTIndex = GetCurrentCTvalue() * (DIM_MAX_VALUE_INDEX / PWM_MAX_DUTY_CYCLE) + P_CdnLightNode->CTEndIndex;

  if(GetTargetCTvalue() > GetCurrentCTvalue())
  {
    g_CDN_LedState.CTFadeFlag = 1;

    if(LightnessAndTempSyncChange == 1)
    {
      return ((GetTargetCTvalue() - GetCurrentCTvalue()) * LIGHT_REMAIN_INTERVAL_HALF + 100);
    }
    else

    {
      return ((GetTargetCTvalue() - GetCurrentCTvalue()) * LIGHT_REMAIN_INTERVAL + 100);
    }
  }
  else if(GetTargetCTvalue() < GetCurrentCTvalue())
  {
    g_CDN_LedState.CTFadeFlag = 0;

    if(LightnessAndTempSyncChange == 1)
    {
      return ((GetCurrentCTvalue() - GetTargetCTvalue()) * LIGHT_REMAIN_INTERVAL_HALF + 100);
    }
    else
    {
      return ((GetCurrentCTvalue() - GetTargetCTvalue()) * LIGHT_REMAIN_INTERVAL + 100);
    }
  }
  else
  {
    g_CDN_LedState.CTFadeFlag = -1;
    TRACE("zx: ChangeToTargetCTRemainTime CT no change\n");
    return 0;
  }
}
//设置色温渐变时间(ms)
void SetCTRemainTime(uint16_t time)
{
  g_CDN_LedState.CTRemainTime = time;
}
//获取色温渐变时间(ms)
uint16_t GetCTRemainTime(void)
{
  return g_CDN_LedState.CTRemainTime;
}
#if CDN_LIGHT_BULB | CDN_LIGHT_STRIP

//设置厂测timer标志位
void SetCdnFactoryTestTimerStarted(uint8_t Flag)
{
	FactoryTimerFlag = Flag;
}
//获取厂测timer标志位
uint8_t GetCdnFactoryTestTimerStarted(void)
{
	return FactoryTimerFlag;
}
//灯厂测步骤：黄光、白光循环高亮
void CdnFactoryTestStep(void)
{
  static uint8_t TickCnt = 0;
  static uint8_t PreTickCnt = 0;
	if(true == GetCdnFactoryTestTimerStarted())
	  {
#if(CDN_MAX_POWER_12W_24W)
      SetLightCurrentValue(CDN_24W_Min_Cur);//电流值设为最小
      SetCurrentLightPowerWorkModeValue(Power_15W);
#else
      SetLightCurrentValue(CDN_12W_Min_Cur);//电流值设为最小
      SetCurrentLightPowerWorkModeValue(Power_5W);
#endif
      TickCnt++;
#if CDN_LIGHT_BULB
      if(CDN_CCT_LIGHT == GetCdnLightDeviceType())
        {
          if(TickCnt == 10)//第一步黄光渐亮
          {
              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, CDN_LIGHTNESS_ZERO);
          }
          else if(TickCnt == 40)//第二步切色温为白光
          {
              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, CDN_LIGHTNESS_MAX);
          }
          else if(TickCnt >= 70)
          {
            TickCnt = 9;
          }
//          else if(TickCnt == 70)//第三步白光渐灭
//          {
//              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, CDN_LIGHTNESS_MAX);
//          }
//          else if(TickCnt == 100)//第四步白光渐亮
//          {
//              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, CDN_LIGHTNESS_MAX);
//          }
//          else if(TickCnt == 130)//第五步切色温为黄光
//          {
//              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, CDN_LIGHTNESS_ZERO);
//          }
//          else if(TickCnt == 160)//第六步黄光渐灭
//          {
//              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, CDN_LIGHTNESS_ZERO);
//          }
//          else if(TickCnt >= 190)
//          {
//            TickCnt = 9;
//          }
        }
      else if(CDN_DIMMING_LIGHT == GetCdnLightDeviceType())
        {
          if(TickCnt == 10)//第一步黄光渐亮
          {
              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, CDN_LIGHTNESS_ZERO);
          }
          else if(TickCnt == 50)//第一步结束变完等待1S进行第二步黄光渐灭
          {
              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, CDN_LIGHTNESS_ZERO);
          }
          else if(TickCnt >= 80)
          {
            TickCnt = 9;
          }
        }
#endif
#if CDN_LIGHT_STRIP
      if(0 == CDN_STRIP_DIMMING_LIGHT)
        {
          if(TickCnt == 10)//第一步黄光渐亮
          {
              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, CDN_LIGHTNESS_ZERO);
          }
          else if(TickCnt == 40)//第二步切色温为白光
          {
              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, CDN_LIGHTNESS_MAX);
          }
          else if(TickCnt >= 70)
          {
            TickCnt = 9;
          }
//          else if(TickCnt == 70)//第三步白光渐灭
//          {
//              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, CDN_LIGHTNESS_MAX);
//          }
//          else if(TickCnt == 100)//第四步白光渐亮
//          {
//              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, CDN_LIGHTNESS_MAX);
//          }
//          else if(TickCnt == 130)//第五步切色温为黄光
//          {
//              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, CDN_LIGHTNESS_ZERO);
//          }
//          else if(TickCnt == 160)//第六步黄光渐灭
//          {
//              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, CDN_LIGHTNESS_ZERO);
//          }
//          else if(TickCnt >= 190)
//          {
//            TickCnt = 9;
//          }
        }
      else if(1 == CDN_STRIP_DIMMING_LIGHT)
        {
          if(TickCnt == 10)//第一步黄光渐亮
          {
              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, CDN_LIGHTNESS_ZERO);
          }
          else if(TickCnt == 50)//第一步结束变完等待1S进行第二步黄光渐灭
          {
              SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, CDN_LIGHTNESS_ZERO);
          }
          else if(TickCnt >= 80)
          {
            TickCnt = 9;
          }
        }
#endif
      PreTickCnt = TickCnt;
    }
	else if(false == GetCdnFactoryTestTimerStarted())
	  {
	    if(GetCurrentLightWorkMode() == CDN_FACTORY_MODE)
	      {
	        TickCnt = 0;
	        if(GetLightState() == CDN_LED_NO_NET)
	          {
//	            TRACE("111111111\n");
	            if(PreTickCnt)
	              {
//	                TRACE("111111111\n");
#if(CDN_MAX_POWER_12W_24W)
      SetLightCurrentValue(CDN_24W_Max_Cur);//电流值设为最大
      //SetCurrentLightPowerWorkModeValue(Power_24W);
#else
      SetLightCurrentValue(CDN_12W_Max_Cur);//电流值设为最大
      //SetCurrentLightPowerWorkModeValue(Power_12W);
#endif
      SetLedState(CDN_LED_NO_NET,100,0);
	                //SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, CDN_LIGHTNESS_ZERO);
	                PreTickCnt = TickCnt;
	              }
	          }
	      }
	  }
}
#endif
//开始厂测
void CdnStartFactoryTest(void)
{
	/* set factory mode */
	//SetCurrentLightWorkMode(CDN_FACTORY_MODE);
	/* indicate factory test start */
	SetLedState(CDN_LED_FACTORY_BLINK,0,0);
	/* start factory timer */
	SetCdnFactoryTestTimerStarted(true);
}
//停止厂测
void CdnStopFactoryTest(void)
{
	/* set work mode */
	//SetCurrentLightWorkMode(CDN_WORK_MODE);
	/* stop factory timer and pwm timer */
  SetLedState(CDN_LED_FACTORY_BLINK,0,0);
	SetCdnFactoryTestTimerStarted(false);
	set_pwm_timer_start_flag(false);
	set_pwm_ct_timer_start_flag(false);
}

void SetKeyDimmingUpDown(uint8_t cmd)
{
  KeyDimmingUpDown = cmd;
}
uint8_t GetKeyDimmingUpDown(void)
{
  return KeyDimmingUpDown;
}
void SetKeyCTUpDowm(uint8_t cmd)
{
  KeyCTUpDowm = cmd;
}
uint8_t GetKeyCTUpDowm(void)
{
  return KeyCTUpDowm;
}
//灯选择PID引脚初始化
void Light_GPIO_Init(void)
{
  //Enable GPIO clock
  CMU_ClockEnable(cmuClock_GPIO, true);
  GPIO_PinModeSet(CDN_CCT_READ_PORT, CDN_CCT_READ_PIN, gpioModeInput, 0);	//灯具PID读取引脚
	GPIO_PinModeSet(CDN_KEY1_PORT, CDN_KEY1_PIN, gpioModeInput, 0);
}
//获取灯选择PID引脚状态
CdnLightDevice GetCdnLightDeviceType(void)
{
	if(1 == GPIO_PinInGet(CDN_CCT_READ_PORT, CDN_CCT_READ_PIN))
	{
		return CDN_CCT_LIGHT;
	}
	else
	{
		return CDN_DIMMING_LIGHT;
	}
}
void CDN_Adv_Type(uint8_t *type)
{
		uint8_t pid[] = {0x84,0x07};

	memcpy(type, pid, sizeof(pid));

}

//设置灯状态
void SetLedState(CdnLedState LedState,uint8_t brightness,uint8_t CTvalue)
{
  g_CDN_LedState.LightState = LedState;
  g_CDN_LedState.Brightness = brightness;
  g_CDN_LedState.CTvalue = CTvalue;
}
//获取灯状态
LightCurrentState GetLedState(void)
{
  return g_CDN_LedState;
}
CdnLedState GetLightState(void)
{
  return g_CDN_LedState.LightState;
}
//灯状态处理，1ms循环
void CdnLedStateHandle(void)
{
  #define BLINKTIME          6         //闪烁次数
  #define RESETBLINKTIME          1         //闪烁次数
	#define FACTORYBLINKTIME   4

  #define SLOWN_BLINK_PERIOD 500        //慢闪烁周期
  #define FACTORY_BLINK_PERIOD 200      //厂测模式闪烁周期

  static uint16_t SlownBlinkCount = 0;  //慢闪烁计数
  static uint16_t FactoryBlinkCount = 0;//厂测模式闪烁计数
	
  static uint8_t ConfigBlinkTime = 0;   //闪烁次数
	static uint8_t FactoryBlinkTime = 0;
	
  static CdnLedState PreLedState = 0;   //记录上一次状态

  SlownBlinkCount++;
	FactoryBlinkCount++;
	 
  if(SlownBlinkCount > SLOWN_BLINK_PERIOD)
  {
    SlownBlinkCount = 0;
    ConfigBlinkTime++;
  }
	if(FactoryBlinkCount > FACTORY_BLINK_PERIOD)
	{
		FactoryBlinkCount = 0;
		FactoryBlinkTime++;
	}
	
  if(ConfigBlinkTime >= BLINKTIME)
  {
    ConfigBlinkTime = BLINKTIME;
  }
	if(FactoryBlinkTime >= FACTORYBLINKTIME)
	{
		FactoryBlinkTime = FACTORYBLINKTIME;
	}

  CdnLedState LightCurrentState = GetLedState().LightState;
  uint8_t LightCurrentBrightness = GetLedState().Brightness;
#if CDN_STRIP_DIMMING_LIGHT
#else
  uint8_t LightCurrentCTvalue = GetLedState().CTvalue;
#endif

  switch(LightCurrentState)
  {
	  case CDN_LED_FACTORY_BLINK:
			if(LightCurrentState != PreLedState)
      {
				FactoryBlinkTime = 0;
        FactoryBlinkCount = 0;
      }
			if(FactoryBlinkCount < (FACTORY_BLINK_PERIOD >> 1))
      {
        GeneralLightnessCtrl(PWM_MAX_DUTY_CYCLE);
        GPIO_PinModeSet(CDN_EN_PORT, CDN_EN_PIN, gpioModePushPull, 1);
      }
      else
      {
        GeneralLightnessCtrl(PWM_MIN_DUTY_CYCLE);
        GPIO_PinModeSet(CDN_EN_PORT, CDN_EN_PIN, gpioModePushPull, 0);
      }
			if(FactoryBlinkTime >= FACTORYBLINKTIME)
			{
				SetLedState(CDN_LED_NO_NET,0,0);
			}
      break;
    case CDN_DIM_CT_LED_ON:
#if CDN_LIGHT_BULB
      if(LightCurrentBrightness != PWM_MIN_DUTY_CYCLE)
      {
        DimLightnessCtrl(LightCurrentBrightness);
      }
      if(LightCurrentCTvalue != PWM_MIN_DUTY_CYCLE)
      {
        CtLightnessCtrl(LightCurrentCTvalue);
      }
#elif CDN_LIGHT_STRIP
#if CDN_STRIP_DIMMING_LIGHT
      CdnLightStripCtrlRepeat(LightCurrentBrightness, PWM_MIN_DUTY_CYCLE);
#else
      CdnLightStripCtrlRepeat(LightCurrentBrightness, LightCurrentCTvalue);
#endif
#endif
      break;
    case CDN_DIM_CT_LED_OFF:
#if CDN_LIGHT_BULB
      DimLightnessCtrl(PWM_MIN_DUTY_CYCLE);
      CtLightnessCtrl(PWM_MIN_DUTY_CYCLE);
      //解决APP全关之后55s左右后灯自动点亮的问题
      GeneralLightnessCtrl(PWM_MIN_DUTY_CYCLE);
#elif CDN_LIGHT_STRIP
      CdnLightStripCtrlRepeat(PWM_MIN_DUTY_CYCLE, PWM_MIN_DUTY_CYCLE);
#endif
      break;
    case CDN_LED_OFF:
      GeneralLightnessCtrl(PWM_MIN_DUTY_CYCLE);
      GPIO_PinModeSet(CDN_EN_PORT, CDN_EN_PIN, gpioModePushPull, 0);
      break;
    case CDN_LED_ON:
      GeneralLightnessCtrl(PWM_MAX_DUTY_CYCLE);
      GPIO_PinModeSet(CDN_EN_PORT, CDN_EN_PIN, gpioModePushPull, 1);
      break;
    case CDN_LED_CONFIG_BLINK:
      if(LightCurrentState != PreLedState)
      {
        ConfigBlinkTime = 0;
        SlownBlinkCount = 0;
      }
      if(SlownBlinkCount < (SLOWN_BLINK_PERIOD >> 1) && ConfigBlinkTime < BLINKTIME)
      {
        GeneralLightnessCtrl(PWM_MIN_DUTY_CYCLE);
        GPIO_PinModeSet(CDN_EN_PORT, CDN_EN_PIN, gpioModePushPull, 0);
      }
      else
      {
        GeneralLightnessCtrl(PWM_MAX_DUTY_CYCLE);
        GPIO_PinModeSet(CDN_EN_PORT, CDN_EN_PIN, gpioModePushPull, 1);
      }
      if(ConfigBlinkTime >= BLINKTIME)
      {
          TRACE("CONFIG_BLINK_END !");
         ConfigBlinkTime = 0;
		     SetLedState(CDN_LED_NO_NET,0,0);
      }
      break;
    case CDN_LED_RESET_BLINK:
      if(LightCurrentState != PreLedState)
      {
        ConfigBlinkTime = 0;
        SlownBlinkCount = 0;
      }
      if(SlownBlinkCount < (SLOWN_BLINK_PERIOD >> 1) && ConfigBlinkTime < RESETBLINKTIME)
      {
        GeneralLightnessCtrl(PWM_MIN_DUTY_CYCLE);
        GPIO_PinModeSet(CDN_EN_PORT, CDN_EN_PIN, gpioModePushPull, 0);
      }
      else
      {
        GeneralLightnessCtrl(PWM_MAX_DUTY_CYCLE);
        GPIO_PinModeSet(CDN_EN_PORT, CDN_EN_PIN, gpioModePushPull, 1);
      }
      if(ConfigBlinkTime >= RESETBLINKTIME)
      {
        SetLedState(CDN_LED_NO_NET,0,0);
      }
      break;
    case CDN_LED_NO_NET:
      GeneralLightnessCtrl(PWM_MAX_DUTY_CYCLE);
      GPIO_PinModeSet(CDN_EN_PORT, CDN_EN_PIN, gpioModePushPull, 1);
      break;
    case CDN_LED_IDLE:
      GeneralLightnessCtrl(PWM_MIN_DUTY_CYCLE);
      break;
		case CDN_LED_NONE_STATE:
			break;
    default:
      break;
  }
  PreLedState = LightCurrentState;
}
//设置灯渐变效果
void SetLightnessCTFadesCtrl(uint8_t Lightness,uint8_t CT)
{
	static uint8_t PreLightness = 0;
	static uint8_t PreCT = 0;

	TRACE("zx: SetLightnessCTFadesCtrl PreLightness:%d,Lightness:%d------PreCT:%d,CT=%d\n",PreLightness,Lightness,PreCT,CT);
	if(PreLightness == Lightness && PreCT == CT){
		TRACE("zx: SetLightnessCTFadesCtrl target lightness and CT same,return!!!\n");
		return;
	}

  //停止渐变timer
  set_pwm_timer_start_flag(false);

  //显示当前亮度和色温
  SetLedState(CDN_DIM_CT_LED_ON,CdnLightNode.CurrentGroup.Lightness,CdnLightNode.CurrentGroup.CT);
	if(Lightness > PWM_MIN_DUTY_CYCLE && Lightness <= PWM_MAX_DUTY_CYCLE){
		CdnLightNode.GroupLightOnOff = LIGHT_ON;
	}
	else{
		CdnLightNode.GroupLightOnOff = LIGHT_OFF;
	}
  TRACE("SetLightnessCTFadesCtrl CurrentLightness = %d,currentCT = %d !\n",GetCurrentBrightness(),GetCurrentCTvalue());

  //设置目标亮度和色温，防御式编程
  if(Lightness > PWM_MAX_DUTY_CYCLE)
  {
    Lightness = PWM_MAX_DUTY_CYCLE;
  }
  else if(CT > PWM_MAX_DUTY_CYCLE)
  {
    CT = PWM_MAX_DUTY_CYCLE;
  }
  SetTargetBrightness(Lightness);
  SetTargetCTvalue(CT);
  TRACE("SetLightnessCTFadesCtrl TargetLightness = %d,TargetCT = %d !\r\n",Lightness,CT);

  //判断亮度和色温是否同时变化
  if(GetCurrentLightWorkMode() == CDN_WORK_MODE)
  {
	  if(Lightness != GetCurrentBrightness() && CT != GetCurrentCTvalue())
	  {
	    LightnessAndTempSyncChange = 1;
	  }
	  else
	  {
	    LightnessAndTempSyncChange = 0;
	  }
  }
	else if(GetCurrentLightWorkMode() == CDN_FACTORY_MODE)
	{
		LightnessAndTempSyncChange = 1;
	}

  //设置亮度和色温的渐变时间
  SetBrightnessRemainTime(ChangeToTargetBrightnessRemainTime());
  SetCTRemainTime(ChangeToTargetCTRemainTime());
  TRACE("Lightness remain time : %d,CT remain time : %d\n",GetBrightnessRemainTime(),GetCTRemainTime());

  //启动渐变timer
  set_pwm_timer_start_flag(true);

	PreLightness = Lightness;
	PreCT = CT;

	//如果上一次目标值不为0，则记录下来留作灯灭再亮起的目标值
	if(PreLightness != 0){
		SetPreTargetBrightness(PreLightness);
	}
}


//灯具呼吸灯效
void CDN_Light_effect_display(void)
{
	switch(Get_CDN_Light_effect_mode())
	{
		case EFFECT_BREATH:
		{
			CDN_Light_Breath_effect();
			break;
		}
		case EFFECT_QUICK_BREATH:
		{
			CDN_Light_Quick_Breath_effect();
			break;
		}
		default:
			break;
	}
}
//设置灯效模式
void Set_CDN_Light_effect_mode(LIGHT_EFFECT mode, uint8_t time)
{
	static uint8_t premode = 0;
	static uint8_t pretime = 0;

	if(premode == mode && pretime == time)
	{
		return;
	}

	LightEffectCMD.mode = mode;
	LightEffectCMD.time = time;

	premode = mode;
	pretime = time;
}
//获取灯效模式
LIGHT_EFFECT Get_CDN_Light_effect_mode(void)
{
	return LightEffectCMD.mode;
}
//获取灯效次数
uint8_t Get_CDN_Light_effect_time(void)
{
	return LightEffectCMD.time;
}

//长按按键调光结束后，组长灯发出当前的亮度和色温
void LightBroadcastCurrentDim(void)
{
  if(CdnLightNode.GroupLeader == 1)
  {
    uint8_t rsp_par[] = {LIGHT_TX_GROUP_LIGHTNESS_CT,0,0,0,0,0};
    rsp_par[1] = (uint8_t)(CdnLightNode.group_adr & 0x00ff);
    rsp_par[2] = (uint8_t)((CdnLightNode.group_adr & 0xff00) >> 8);
    rsp_par[3] = CdnLightNode.CurrentGroup.Lightness;  //当前亮度值
    rsp_par[4] = CdnLightNode.CurrentGroup.CT;         //当前色温值

    if(rsp_par[3] == PWM_MAX_DUTY_CYCLE)
    {
      rsp_par[5] = DIMMING_MAX;//最大亮度
    }
    else if(rsp_par[3] == PWM_MIN_DUTY_CYCLE)
    {
      rsp_par[5] = DIMMING_MIN;//最小亮度
    }
    else
    {
      rsp_par[5] = ADJUST_END_VALUE;
    }
    TRACE("LightBroadcaseCurrentDimCT GroupAdr = 0x%02x%02x,Lightness = %d,CT = %d,par[5] = %d \r\n",rsp_par[1],rsp_par[2],rsp_par[3],rsp_par[4],rsp_par[5]);
    sl_status_t send_sc = sl_btmesh_vendor_model_send(BROADCASE_ADDRESS,0,0,0,0x0211,0,0,CDN_VENDOR_OP_SET,1,sizeof(rsp_par),rsp_par);
    if(send_sc == SL_STATUS_OK)
    {
      TRACE("LightBroadcaseCurrentDimCT Success !\r\n");
			cdn_get_node_primary_element_seqnum();
    }
    else
    {
      TRACE("LightBroadcaseCurrentDimCT Fail SC = 0x%lx \r\n",send_sc);
    }
  }
}

//长按按键调色结束后，组长灯发出当前的亮度和色温
void LightBroadcastCurrentCt(void)
{
  if(CdnLightNode.GroupLeader == 1)
  {
    uint8_t rsp_par[] = {LIGHT_TX_GROUP_LIGHTNESS_CT,0,0,0,0,0};
    rsp_par[1] = (uint8_t)(CdnLightNode.group_adr & 0x00ff);
    rsp_par[2] = (uint8_t)((CdnLightNode.group_adr & 0xff00) >> 8);
    rsp_par[3] = CdnLightNode.CurrentGroup.Lightness;  //当前亮度值
    rsp_par[4] = CdnLightNode.CurrentGroup.CT;         //当前色温值
    if(rsp_par[4] == PWM_MAX_DUTY_CYCLE)
    {
      rsp_par[5] = COLORTEMP_MAX;//最大色温
    }
    else if(rsp_par[4] == PWM_MIN_DUTY_CYCLE)
    {
      rsp_par[5] = COLORTEMP_MIN;//最小色温
    }
    else
    {
      rsp_par[5] = ADJUST_END_VALUE;
    }
    TRACE("LightBroadcaseCurrentDimCT GroupAdr = 0x%02x%02x,Lightness = %d,CT = %d,par[5] = %d \r\n",rsp_par[1],rsp_par[2],rsp_par[3],rsp_par[4],rsp_par[5]);
    sl_status_t send_sc = sl_btmesh_vendor_model_send(BROADCASE_ADDRESS,0,0,0,0x0211,0,0,CDN_VENDOR_OP_SET,1,sizeof(rsp_par),rsp_par);
    if(send_sc == SL_STATUS_OK)
    {
      TRACE("LightBroadcaseCurrentDimCT Success !\r\n");
			//cdn_get_node_primary_element_seqnum();
    }
    else
    {
      TRACE("LightBroadcaseCurrentDimCT Fail SC = 0x%lx \r\n",send_sc);
    }
  }
}

//长按按键控制组内灯调光
void LightnessDimmingCtrlByKey(uint16_t GroupAdr, uint8_t Cmd)
{
	if(ERROR == is_valid_group(GroupAdr))
  {
    TRACE("zx: GroupAdr = 0x%x,return!!!\n",GroupAdr);
    return;
  }
  if(CdnLightNode.group_adr == GroupAdr)
  {
    TRACE("zx: LightnessDimmingCtrlByKey cmd = %d\n",Cmd);
    switch(Cmd)
    {
      case DIMMING_UP:
        SetKeyDimmingUpDown(DIMMING_UP);
        SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, GetCurrentCTvalue());
        break;

      case DIMMING_DOWN:
        SetKeyDimmingUpDown(DIMMING_DOWN);
        SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MIN, GetCurrentCTvalue());
        break;

      case ADJUST_END:
        SetKeyDimmingUpDown(ADJUST_END);
        SetPreTargetBrightness(GetCurrentBrightness());//调光结束后记录全关再全开后要达到的目标值     
        set_pwm_timer_start_flag(false);
        SetLightnessCTFadesCtrl(GetCurrentBrightness(), GetCurrentCTvalue());//调光结束后同步一下PreLightness值 
        LightBroadcastCurrentDim();
        break;

      default:
        break;
    }
  }
  else
  {
    TRACE("zx: %s,Adr different\n",__FUNCTION__);
  }
}
//长按色温按键控制灯调色
void LightnessCCTCtrlByKey(uint16_t GroupAddr,uint8_t CCT_Cmd,uint8_t AdjRoomArea)
{
  TRACE("zx: LightnessCCTCtrlByKey current light group addr : 0x%x, CCT_Cmd = %d,AdjRoomArea = %d\n",CdnLightNode.group_adr,CCT_Cmd,AdjRoomArea);

	if(CdnLightNode.group_adr == GroupAddr || CdnLightNode.LightRoomArea == AdjRoomArea)
	{
		switch(CCT_Cmd)
    {
      case ADJUST_CT_UP:
        SetKeyCTUpDowm(ADJUST_CT_UP);
#if CDN_LIGHT_BULB | CDN_LIGHT_STRIP				
        SetLightnessCTFadesCtrl(GetCurrentBrightness(), CDN_LIGHTNESS_MAX);
#endif
        break;

      case ADJUST_CT_DOWN:
        SetKeyCTUpDowm(ADJUST_CT_DOWN);
#if CDN_LIGHT_BULB | CDN_LIGHT_STRIP				
        SetLightnessCTFadesCtrl(GetCurrentBrightness(), CDN_LIGHTNESS_ZERO);
#endif
        break;

      case ADJUST_END:
        SetKeyCTUpDowm(ADJUST_END);
        SetTargetCTvalue(GetCurrentCTvalue());
#if CDN_LIGHT_BULB | CDN_LIGHT_STRIP				
        set_pwm_ct_timer_start_flag(false);
				SetLightnessCTFadesCtrl(GetCurrentBrightness(), GetCurrentCTvalue());//调光结束后同步一下PreCT值
				LightBroadcastCurrentCt();
#endif				
        break;

      default:
        break;
    }
	}
}
//短按色温按键控制灯调色
void LightCTChangeByKeyClick(uint16_t GroupAddr, uint8_t CCT_Cmd,uint8_t AdjRoomArea)
{
	TRACE("zx: LightCTChangeByKeyClick current light group addr : 0x%x, CCT_Cmd = %d，AdjRoomArea = %d\n",CdnLightNode.group_adr,CCT_Cmd,AdjRoomArea);

	//组地址相同或者房间相同
	if(CdnLightNode.group_adr == GroupAddr || CdnLightNode.LightRoomArea == AdjRoomArea)
	{
		switch(CCT_Cmd)
		{
			case LIGHT_ON:
				SetKeyCTUpDowm(ADJUST_CT_UP);
#if CDN_LIGHT_BULB | CDN_LIGHT_STRIP			
				if(GetBrightnessRemainTime() != 0) //此时当前组还在调光中
				{
					SetLightnessCTFadesCtrl(GetTargetBrightness(), CDN_LIGHTNESS_MAX);
				}
				else //此时当前组没有调光，只进行色温开关
				{
					SetLightnessCTFadesCtrl(GetCurrentBrightness(), CDN_LIGHTNESS_MAX);
				}
#endif
				break;

			case LIGHT_OFF:
				SetKeyCTUpDowm(ADJUST_CT_DOWN);
#if CDN_LIGHT_BULB | CDN_LIGHT_STRIP	
				if(GetBrightnessRemainTime() != 0) //此时当前组还在调光中
				{
					SetLightnessCTFadesCtrl(GetTargetBrightness(), CDN_LIGHTNESS_ZERO);
				}
				else //此时当前组没有调光，只进行色温开关
				{
					SetLightnessCTFadesCtrl(GetCurrentBrightness(), CDN_LIGHTNESS_ZERO);
				}
#endif
				break;

			default:
				break;
		}
	}
}

//同步亮度和色温
void GroupLightnessCCTSync(uint16_t GroupAdr,uint8_t Lightness,uint8_t CT)
{
  if(CdnLightNode.GroupLeader == 0) //不是组长才需要同步
  {
    if(CdnLightNode.group_adr == GroupAdr)  //判断是否是自己所在的组
    {
      //同步亮度和色温
      TRACE("zsm: GroupLightnessCCTSync Lightness = %d CT = %d \r\n",Lightness,CT);
      CdnLightNode.CurrentGroup.Lightness = Lightness;
      CdnLightNode.CurrentGroup.CT = CT;
    }
  }
}

//设置入网标志
void SetProvisionFlag(uint8_t provisioned)
{
  provision_mode = provisioned;
}
//获取入网标志
uint8_t GetProvisionFlag(void)
{
  return provision_mode;
}
//查询某个组是否有组长
void CheckGroupLeaderAck(uint16_t GroupAdr,uint16_t adr_src)
{
	if(ERROR == is_valid_group(GroupAdr))
  {
    return;
  }
  TRACE("Enter CheckGroupLeaderAck GroupAdr = 0x%x,current group_adr = 0x%x \r\n",GroupAdr,CdnLightNode.group_adr);
  if(CdnLightNode.group_adr == GroupAdr)
  {
    TRACE("zx: CdnLightNode.GroupLeader = %d\n",CdnLightNode.GroupLeader);
    if(CdnLightNode.GroupLeader == 1)
    {
      uint8_t rsp_par[] = {LIGHT_TX_GROUP_LEADER_ACK,0,0,0,0,0};
      rsp_par[1] = (uint8_t)(CdnLightNode.group_adr & 0x00ff);
      rsp_par[2] = (uint8_t)((CdnLightNode.group_adr & 0xff00) >> 8);
      rsp_par[3] = CdnLightNode.CurrentGroup.Lightness;  //当前亮度值
      rsp_par[4] = CdnLightNode.CurrentGroup.CT;         //当前色温值
      rsp_par[5] = CdnLightNode.GroupLightOnOff;				 //当前灯开关状态

      TRACE("CheckGroupLeaderAck GroupAdr = 0x%x%x,Lightness = %d,CT = %d,LightOnOff = %d\r\n",rsp_par[2],rsp_par[1],rsp_par[3],rsp_par[4],rsp_par[5]);
      sl_status_t send_sc = sl_btmesh_vendor_model_send(adr_src,0,0,0,0x0211,0,0,CDN_VENDOR_OP_SET,1,sizeof(rsp_par),rsp_par);
      if(send_sc == SL_STATUS_OK)
      {
          TRACE("CheckGroupLeaderAck Success !\r\n");
					cdn_get_node_primary_element_seqnum();
      }
      else
      {
          TRACE("CheckGroupLeaderAck Fail SC = 0x%lx \r\n",send_sc);
      }
    }
  }
}

//灯分组
void LedGroupBind(uint16_t GroupAdr,uint8_t GroupLeader,uint8_t RoomAreaBind)
{
	TRACE("zx: LedGroupBind GroupAdr:0x%x,GroupLeader:%d\n",GroupAdr,GroupLeader);
  if(ERROR == is_valid_group(GroupAdr))
  {
    return;
  }
	
  CdnLightNode.group_adr = GroupAdr;
	CdnLightNode.GroupLeader = GroupLeader;
  CdnLightNode.LightRoomArea = RoomAreaBind;
  SetProvisionFlag(1);        						 //灯分组成功才算成功入网
  EnterGroupSuccess();
	SetCurrentLightWorkMode(CDN_WORK_MODE);  //入网成功后设置为工作模式
  SetLedState(CDN_LED_IDLE,0,0);  				 //入网并且分组成功灭灯
  SetSaveFlashFlag(LIGHT_INFO_KEY_ID59);	 //保存flash
  TRACE("LedGroupBind GroupAdr = 0x%x,GroupLeader = %d \r\n",CdnLightNode.group_adr,CdnLightNode.GroupLeader);
}

//返回该灯是否是组长
void LedInfoAck(uint16_t adr_src)
{
  uint8_t rsp_par[] = {LIGHT_TX_GROUP_IF_LEADER,0,0,0};
  rsp_par[1] = (uint8_t)(CdnLightNode.group_adr & 0x00ff);
  rsp_par[2] = (uint8_t)((CdnLightNode.group_adr & 0xff00) >> 8);
  rsp_par[3] = CdnLightNode.GroupLeader;  //组长信息
  TRACE("LedInfoAck CdnLightNode.GroupLeader = %d !\r\n",CdnLightNode.GroupLeader);
  sl_status_t send_sc = sl_btmesh_vendor_model_send(adr_src,0,0,0,0x0211,0,0,CDN_VENDOR_OP_SET,1,sizeof(rsp_par),rsp_par);
  if(send_sc == SL_STATUS_OK)
  {
      TRACE("LedInfoAck Success !\r\n");
			cdn_get_node_primary_element_seqnum();
  }
  else
  {
      TRACE("LedInfoAck Fail SC = 0x%lx \r\n",send_sc);
  }
}

//APP设置某组灯的亮度和色温
void SetCurrentGroupLightnessCT(uint16_t GroupAdr,uint8_t Lightness,uint8_t CT)
{
	if(ERROR == is_valid_group(GroupAdr))
  {
    return;
  }
  if(CdnLightNode.group_adr == GroupAdr)
  {
    SetLightnessCTFadesCtrl(Lightness, CT);
  }
}

//语音调节某组灯光色温
void VoiceSetGroupLightnessCtAdj(uint16_t GroupAdr,uint8_t Lightness,uint8_t CT)
{
	if(ERROR == is_valid_group(GroupAdr))
  {
    return;
  }
  if(CdnLightNode.group_adr == GroupAdr)
  {
    SetLightnessCTFadesCtrl(Lightness, CT);
  }
}

//语音调节某房间的灯光色温
void VoiceSetRoomLightnessCtAdj(uint8_t RoomId,uint8_t Lightness,uint8_t CT)
{
  if(CdnLightNode.LightRoomArea == RoomId)
  {
    SetLightnessCTFadesCtrl(Lightness, CT);
  }
}


//按下的是开或者关,设为1
void LedCurrentGroupOnJudge(uint16_t GroupAdr)
{
  if(ERROR == is_valid_group(GroupAdr))
  {
    return;
  }
  if(CdnLightNode.group_adr == GroupAdr)
  {
      SetGroupOnoffJudgeFlag(1);
  }
}
//按下的是亮度条,设为0
void LedCurrentGroupOffJudge(uint16_t GroupAdr)
{
  if(ERROR == is_valid_group(GroupAdr))
  {
    return;
  }
  if(CdnLightNode.group_adr == GroupAdr)
  {
      SetGroupOnoffJudgeFlag(0);
  }
}

//控制某组灯亮灭
void LedCurrentGroupOnOffCtrl(uint16_t GroupAdr,uint8_t OnOff)
{
	if(ERROR == is_valid_group(GroupAdr))
  {
    return;
  }	

  if(CdnLightNode.group_adr == GroupAdr)
  {			
    if(OnOff)
    {
      if(GetPreTargetBrightness() == CDN_LIGHTNESS_ZERO)
      {
				if(GetCTRemainTime() != 0)
				{
					SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, GetTargetCTvalue());
				}
				else
				{
        	SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, GetCurrentCTvalue());
				}
      }
      else
      {
				if(GetCTRemainTime() != 0)
				{
					SetLightnessCTFadesCtrl(GetPreTargetBrightness(), GetTargetCTvalue());
				}
				else
				{
        	SetLightnessCTFadesCtrl(GetPreTargetBrightness(), GetCurrentCTvalue());
				}
      }
    }
    else
    {
			if(GetCTRemainTime() != 0)
			{
				SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, GetTargetCTvalue());
			}
			else
			{
      	SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, GetCurrentCTvalue());
			}
    }
		CdnLightNode.GroupLightOnOff = OnOff;
    TRACE("LedCurrentGroupOnOffCtrl GroupAdr = 0x%x,OnOff = %d \r\n",GroupAdr,OnOff);
  }
}

//语音控制所有灯亮灭
void VoiceCtrlAllBulbOnOff(uint8_t OnOff)
{
	if(OnOff)
	{
		if(GetPreTargetBrightness() == CDN_LIGHTNESS_ZERO)
		{
			if(GetCTRemainTime() != 0)
			{
				SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, GetTargetCTvalue());
			}
			else
			{
				SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, GetCurrentCTvalue());
			}
		}
		else
		{
			if(GetCTRemainTime() != 0)
			{
				SetLightnessCTFadesCtrl(GetPreTargetBrightness(), GetTargetCTvalue());
			}
			else
			{
				SetLightnessCTFadesCtrl(GetPreTargetBrightness(), GetCurrentCTvalue());
			}
		}
	}
	else
	{
		if(GetCTRemainTime() != 0)
		{
			SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, GetTargetCTvalue());
		}
		else
		{
			SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, GetCurrentCTvalue());
		}
	}
	CdnLightNode.GroupLightOnOff = OnOff;
	TRACE("VoiceCtrlAllBulbOnOff OnOff = %d \r\n",OnOff);
}

//控制某房间灯亮灭
void LedCurrentRoomOnOffCtrl(uint8_t RoomId,uint8_t OnOff)
{
  if(CdnLightNode.LightRoomArea == RoomId)
  {			
    if(OnOff)
    {
      if(GetPreTargetBrightness() == CDN_LIGHTNESS_ZERO)
      {
				if(GetCTRemainTime() != 0)
				{
					SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, GetTargetCTvalue());
				}
				else
				{
        	SetLightnessCTFadesCtrl(CDN_LIGHTNESS_MAX, GetCurrentCTvalue());
				}
      }
      else
      {
				if(GetCTRemainTime() != 0)
				{
					SetLightnessCTFadesCtrl(GetPreTargetBrightness(), GetTargetCTvalue());
				}
				else
				{
        	SetLightnessCTFadesCtrl(GetPreTargetBrightness(), GetCurrentCTvalue());
				}
      }
    }
    else
    {
			if(GetCTRemainTime() != 0)
			{
				SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, GetTargetCTvalue());
			}
			else
			{
      	SetLightnessCTFadesCtrl(CDN_LIGHTNESS_ZERO, GetCurrentCTvalue());
			}
    }
		CdnLightNode.GroupLightOnOff = OnOff;
    TRACE("LedCurrentRoomOnOffCtrl RoomId = %d,OnOff = %d \r\n",RoomId,OnOff);
  }
}

//把组从场景内删除
void DeleteCurrentGroupSceneId(uint8_t Sceneid,uint16_t GroupAdr)
{
  TRACE("zsm: DeleteCurrentGroupSceneId Sceneid = %d,GroupAdr = 0x%x \r\n", Sceneid,GroupAdr);
  if(ERROR == is_valid_scene(Sceneid))
    return;

	//确定场景删除的组是自己所在的组
	if(CdnLightNode.group_adr == GroupAdr)
	{
		//清空当前的场景ID
		SceneArray[Sceneid - 1].GroupInScene.SceneID = 0;
		//清空记录此场景的亮度和色温
		SceneArray[Sceneid - 1].GroupInScene.Lightness = 0;
    SceneArray[Sceneid - 1].GroupInScene.CT = 0;
		//当删除场景组信息时要进行存Flash
		SetSaveFlashFlag(Sceneid);
	}
}
//设置某个场景下关联的组的亮度和色温
void SetSceneGroupLightnessCT(uint8_t Sceneid,uint16_t GroupAdr,uint8_t Lightness, uint8_t CT)
{	
  if(ERROR == is_valid_scene(Sceneid) || ERROR == is_valid_group(GroupAdr))
	{
		TRACE("zx: SetSceneGroupLightnessCT set error!!!\n");
    return;
  }

	//判断灯当前组地址是否是该场景下发的组地址
	if(CdnLightNode.group_adr == GroupAdr)
	{
		TRACE("zx: SetSceneGroupLightnessCT Sceneid = %d,GroupAdr = 0x%x,Lightness = %d,CT = %d \r\n",Sceneid,GroupAdr,Lightness,CT);
		//输出调光值到灯
		SetLedState(CDN_DIM_CT_LED_ON,Lightness,CT);
		//同步当前的亮度和色温
		SetCurrentGroupLightnessCT(GroupAdr,Lightness,CT);
	}
}
//确认保存场景信息
void ConfirmSceneGroupLightnessCT(uint8_t Sceneid,uint16_t GroupAdr,uint8_t Lightness, uint8_t CT)
{
	if(ERROR == is_valid_scene(Sceneid) || ERROR == is_valid_group(GroupAdr))
	{
		TRACE("zx: SetSceneGroupLightnessCT set error!!!\n");
		return;
	}

	//判断灯当前组地址是否是该场景下发的组地址
	if(CdnLightNode.group_adr == GroupAdr)
	{
		TRACE("zx: ConfirmSceneGroupLightnessCT Sceneid = %d,GroupAdr = 0x%x,Lightness = %d,CT = %d \r\n",Sceneid,GroupAdr,Lightness,CT);
		SceneArray[Sceneid - 1].GroupInScene.SceneID = Sceneid;
		SceneArray[Sceneid - 1].GroupInScene.Lightness = Lightness;
		SceneArray[Sceneid - 1].GroupInScene.CT = CT;
	}
	//存储场景信息到flash
	SetSaveFlashFlag(Sceneid);
	
}

#if SCENE_POWER_OFF_MEMORY
static uint8_t LightSceneMemoryID = 0;  //场景断电记忆----当前场景
static uint8_t PreLightSceneMemoryID = 0;  //场景断电记忆----上一次场景
static uint8_t FirstPowerOnFlag = 0;   //首次上电
void SetLightSceneMemoryID(uint8_t Sceneid)
{
  LightSceneMemoryID = Sceneid;
}

void SetPreLightSceneMemoryID(uint8_t Sceneid)
{
  PreLightSceneMemoryID = Sceneid;
}

void SetFirstPowerOnFlag(uint8_t Flag)
{
  FirstPowerOnFlag = Flag;
}


void CheckLightSceneInfoIfChange(void)  //场景有变化且渐变完成，保存场景ID
{
  if(LightSceneMemoryID != PreLightSceneMemoryID)
    {
      //TRACE("CurrentBrightness = %d, TargetBrightness = %d\n",GetCurrentBrightness(),GetTargetBrightness());
      if(GetCurrentBrightness() == GetTargetBrightness())
        {
          cdn_nvm3_delete(SCENE_MEMORY_KEY_ID61);
          cdn_nvm3_write(SCENE_MEMORY_KEY_ID61,(uint8_t *)(&LightSceneMemoryID),sizeof(LightSceneMemoryID));
          SetPreLightSceneMemoryID(LightSceneMemoryID);
        }
    }
}

void ExecuteMemorySceneId(void)  //首次上电后执行保存的场景
{
  static uint16_t SceneMemoryTime = 0;
  if(FirstPowerOnFlag)
    {
      SceneMemoryTime ++;
      if(SceneMemoryTime >50)
        {
          if(LightSceneMemoryID)
            {
              SetCurrentCTvalue(SceneArray[LightSceneMemoryID - 1].GroupInScene.CT);
              SceneRecall(LightSceneMemoryID,1);
            }
          SetFirstPowerOnFlag(0);
          SceneMemoryTime = 0;
        }
    }
}

void LightSceneMemoryInit(void)
{
  SetPreLightSceneMemoryID(LightSceneMemoryID);
  SetFirstPowerOnFlag(1);
}
#endif

//按键或者APP调用场景
void SceneRecall(uint8_t Sceneid,uint8_t OnOff)
{
  if(ERROR == is_valid_scene(Sceneid))
	{
		TRACE("zx: SceneRecall Sceneid error!!!\n");
    return;
  }
#if CDN_DEBUG_TRACE
	uint8_t Groupid = (uint8_t)(CdnLightNode.group_adr & 0xff);
	TRACE("zx: SceneRecall Sceneid = %d,OnOff = %d,GroupID = %d\n",Sceneid, OnOff, Groupid);
#endif
	//判断该场景ID下的组信息是否有自己有关
	if(SceneArray[Sceneid - 1].GroupInScene.SceneID == Sceneid)
	{
    SetGroupOnoffJudgeFlag(1);
		if(OnOff)
    {
			//打开场景，渐变亮
      SetCurrentGroupLightnessCT(CdnLightNode.group_adr,
                                 SceneArray[Sceneid - 1].GroupInScene.Lightness,
                                 SceneArray[Sceneid - 1].GroupInScene.CT);  //同步当前的亮度和色温
      TRACE("zx: SceneRecall Sceneid = %d,GroupAdr = %d,Lightness = %d,CT = %d \r\n",
      Sceneid,Groupid,SceneArray[Sceneid - 1].GroupInScene.Lightness,
      SceneArray[Sceneid - 1].GroupInScene.CT);
    }
    else
    {
      //关闭场景，渐变灭		          
      SetLightnessCTFadesCtrl(0, GetCurrentCTvalue());
    }

#if SCENE_POWER_OFF_MEMORY
		SetLightSceneMemoryID(Sceneid);
#endif
	}
}
//删除场景
void DeleteCurrentSceneid(uint8_t Sceneid)
{
	TRACE("zx: DeleteCurrentSceneid Sceneid = %d \r\n", Sceneid);
	if(ERROR == is_valid_scene(Sceneid))
    return;

	//如果此场景已清空，则不再进行操作Flash
	if(SceneArray[Sceneid - 1].GroupInScene.SceneID == 0)
		return;

	//清空当前的场景ID
	SceneArray[Sceneid - 1].GroupInScene.SceneID = 0;
	//清空记录此场景的亮度和色温
	SceneArray[Sceneid - 1].GroupInScene.Lightness = 0;
  SceneArray[Sceneid - 1].GroupInScene.CT = 0;
	//当删除场景组信息时要进行存Flash
	SetSaveFlashFlag(Sceneid);
}

//删除组
void DeleteGroup(uint16_t GroupAdr,uint8_t Reset)
{
	TRACE("zx: DeleteGroup,GroupAdr:0x%04x,Rest:%d,Current_Adr:0x%04x\n",GroupAdr,Reset,CdnLightNode.group_adr);
	if(GroupAdr == 0x0000 || GroupAdr > LIGHT_MAX_GROUP_NUM)
  {
    return;
  }
  if(CdnLightNode.group_adr == GroupAdr)
  {
    if(Reset)
    {
      sl_btmesh_initiate_node_reset();    //清除信息并重启
    }
  }
}
//入组中
void EnteringGroup(void)
{
  EnterGroupFailFlag = 1;
}
//获取入组进程
uint8_t GetEnterGroupInfo(void)
{
  return EnterGroupFailFlag;
}
//入组成功
void EnterGroupSuccess(void)
{
  EnterGroupFailFlag = 0;
}

#if CDN_LIGHT_BULB | CDN_LIGHT_STRIP
//设置搜索时闪烁标志
void SetConfigBlinkFlag(uint8_t Flag)
{
  ConfigBlinkFlag = Flag;
}
//获取搜索时闪烁标志
uint8_t GetConfigBlinkFlag(void)
{
  return ConfigBlinkFlag;
}
#endif
//入网分组失败后重启,1000ms循环判断
void FailedToEnterNetworkGroup(void)
{
#if CDN_LIGHT_BULB | CDN_LIGHT_STRIP
  static uint8_t ConfigTimeCnt = 0;

  if(GetConfigBlinkFlag() == 1)
    {
      ConfigTimeCnt++;
      if(ConfigTimeCnt > 3)//3S一直广播发送闪烁指令
        {
          ConfigTimeCnt = 0;
          SetConfigBlinkFlag(0);
          TRACE("ConfigTime = 3S!! \r\n");
        }
    }
#endif




	//设置蓝牙配网超时重置时间为120s
  #define REBOOT_TIME 120
  #define BLINK_TIME_OUT 125

  static uint8_t FailTimeCnt = 0;
  if(GetEnterGroupInfo())
  {
    FailTimeCnt++;
    if(FailTimeCnt > REBOOT_TIME)
    {
      TRACE("FailedToEnterNetworkGroup \r\n");
      SetLedState(CDN_LED_CONFIG_BLINK,0,0);    //闪烁
      if(FailTimeCnt >= BLINK_TIME_OUT)
      {
        SetLedState(CDN_LED_OFF,0,0);
        FailTimeCnt = 0;
        sl_btmesh_initiate_node_reset();  //清除信息并重启
      }
    }
  }
  else
  {
    FailTimeCnt = 0;
  }
}

//设置要保存的场景ID标志
void SetSaveFlashFlag(uint8_t FlashFlag)
{
  TRACE("zsm: SetSaveFlashFlag FlashFlag = %d \r\n",FlashFlag);
  FlashSaveFlag = FlashFlag;
}

//获取要保存的场景ID标志
uint8_t GetSaveFlashFlag(void)
{
  return FlashSaveFlag;
}

//检测灯信息是否有变化
void CheckLightInfoIfChange(void)
{
	switch(GetSaveFlashFlag())
	{
		case LIGHT_INFO_KEY_ID59:
		{
			cdn_nvm3_delete(LIGHT_INFO_KEY_ID59);
			cdn_nvm3_write(LIGHT_INFO_KEY_ID59,(uint8_t *)(&CdnLightNode),sizeof(CdnLightNode));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zx: CheckLightInfoIfChange LIGHT_INFO_KEY_ID59 data len = %d \r\n",sizeof(CdnLightNode));
		}
		break;
		case SCENE_INFO_KEY_ID1:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID1);
			cdn_nvm3_write(SCENE_INFO_KEY_ID1,(uint8_t *)(&SceneArray[0]),sizeof(SceneArray[0]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 1] len = %d \r\n",sizeof(SceneArray[0]));
	
		}
		break;
		case SCENE_INFO_KEY_ID2:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID2);
			cdn_nvm3_write(SCENE_INFO_KEY_ID2,(uint8_t *)(&SceneArray[1]),sizeof(SceneArray[1]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 2] len = %d \r\n",sizeof(SceneArray[1]));
	
		}
		break;
		case SCENE_INFO_KEY_ID3:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID3);
			cdn_nvm3_write(SCENE_INFO_KEY_ID3,(uint8_t *)(&SceneArray[2]),sizeof(SceneArray[2]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 3] len = %d \r\n",sizeof(SceneArray[2]));
	
		}
		break;
		case SCENE_INFO_KEY_ID4:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID4);
			cdn_nvm3_write(SCENE_INFO_KEY_ID4,(uint8_t *)(&SceneArray[3]),sizeof(SceneArray[3]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 4] len = %d \r\n",sizeof(SceneArray[3]));
	
		}
		break;
		case SCENE_INFO_KEY_ID5:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID5);
			cdn_nvm3_write(SCENE_INFO_KEY_ID5,(uint8_t *)(&SceneArray[4]),sizeof(SceneArray[4]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 5] len = %d \r\n",sizeof(SceneArray[4]));
	
		}
		break;
		case SCENE_INFO_KEY_ID6:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID6);
			cdn_nvm3_write(SCENE_INFO_KEY_ID6,(uint8_t *)(&SceneArray[5]),sizeof(SceneArray[5]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 6] len = %d \r\n",sizeof(SceneArray[5]));
	
		}
		break;
		case SCENE_INFO_KEY_ID7:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID7);
			cdn_nvm3_write(SCENE_INFO_KEY_ID7,(uint8_t *)(&SceneArray[6]),sizeof(SceneArray[6]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 7] len = %d \r\n",sizeof(SceneArray[6]));
	
		}
		break;
		case SCENE_INFO_KEY_ID8:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID8);
			cdn_nvm3_write(SCENE_INFO_KEY_ID8,(uint8_t *)(&SceneArray[7]),sizeof(SceneArray[7]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 8] len = %d \r\n",sizeof(SceneArray[7]));
	
		}
		break;
		case SCENE_INFO_KEY_ID9:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID9);
			cdn_nvm3_write(SCENE_INFO_KEY_ID9,(uint8_t *)(&SceneArray[8]),sizeof(SceneArray[8]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 9] len = %d \r\n",sizeof(SceneArray[8]));
	
		}
		break;
		case SCENE_INFO_KEY_ID10:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID10);
			cdn_nvm3_write(SCENE_INFO_KEY_ID10,(uint8_t *)(&SceneArray[9]),sizeof(SceneArray[9]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 10] len = %d \r\n",sizeof(SceneArray[9]));
	
		}
		break;
		case SCENE_INFO_KEY_ID11:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID11);
			cdn_nvm3_write(SCENE_INFO_KEY_ID11,(uint8_t *)(&SceneArray[10]),sizeof(SceneArray[10]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 11] len = %d \r\n",sizeof(SceneArray[10]));
	
		}
		break;
		case SCENE_INFO_KEY_ID12:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID12);
			cdn_nvm3_write(SCENE_INFO_KEY_ID12,(uint8_t *)(&SceneArray[11]),sizeof(SceneArray[11]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 12] len = %d \r\n",sizeof(SceneArray[11]));
	
		}
		break;
		case SCENE_INFO_KEY_ID13:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID13);
			cdn_nvm3_write(SCENE_INFO_KEY_ID13,(uint8_t *)(&SceneArray[12]),sizeof(SceneArray[12]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 13] len = %d \r\n",sizeof(SceneArray[12]));
	
		}
		break;
		case SCENE_INFO_KEY_ID14:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID14);
			cdn_nvm3_write(SCENE_INFO_KEY_ID14,(uint8_t *)(&SceneArray[13]),sizeof(SceneArray[13]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 14] len = %d \r\n",sizeof(SceneArray[13]));
	
		}
		break;
		case SCENE_INFO_KEY_ID15:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID15);
			cdn_nvm3_write(SCENE_INFO_KEY_ID15,(uint8_t *)(&SceneArray[14]),sizeof(SceneArray[14]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 15] len = %d \r\n",sizeof(SceneArray[14]));
	
		}
		break;
		case SCENE_INFO_KEY_ID16:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID16);
			cdn_nvm3_write(SCENE_INFO_KEY_ID16,(uint8_t *)(&SceneArray[15]),sizeof(SceneArray[15]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 16] len = %d \r\n",sizeof(SceneArray[15]));
	
		}
		break;
		case SCENE_INFO_KEY_ID17:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID17);
			cdn_nvm3_write(SCENE_INFO_KEY_ID17,(uint8_t *)(&SceneArray[16]),sizeof(SceneArray[16]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 17] len = %d \r\n",sizeof(SceneArray[16]));
	
		}
		break;
		case SCENE_INFO_KEY_ID18:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID18);
			cdn_nvm3_write(SCENE_INFO_KEY_ID18,(uint8_t *)(&SceneArray[17]),sizeof(SceneArray[17]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 18] len = %d \r\n",sizeof(SceneArray[17]));
	
		}
		break;
		case SCENE_INFO_KEY_ID19:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID19);
			cdn_nvm3_write(SCENE_INFO_KEY_ID19,(uint8_t *)(&SceneArray[18]),sizeof(SceneArray[18]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 19] len = %d \r\n",sizeof(SceneArray[18]));
	
		}
		break;
		case SCENE_INFO_KEY_ID20:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID20);
			cdn_nvm3_write(SCENE_INFO_KEY_ID20,(uint8_t *)(&SceneArray[19]),sizeof(SceneArray[19]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 20] len = %d \r\n",sizeof(SceneArray[19]));
	
		}
		break;
		case SCENE_INFO_KEY_ID21:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID21);
			cdn_nvm3_write(SCENE_INFO_KEY_ID21,(uint8_t *)(&SceneArray[20]),sizeof(SceneArray[20]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 21] len = %d \r\n",sizeof(SceneArray[20]));
	
		}
		break;
		case SCENE_INFO_KEY_ID22:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID22);
			cdn_nvm3_write(SCENE_INFO_KEY_ID22,(uint8_t *)(&SceneArray[21]),sizeof(SceneArray[21]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 22] len = %d \r\n",sizeof(SceneArray[21]));
	
		}
		break;
		case SCENE_INFO_KEY_ID23:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID23);
			cdn_nvm3_write(SCENE_INFO_KEY_ID23,(uint8_t *)(&SceneArray[22]),sizeof(SceneArray[22]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 23] len = %d \r\n",sizeof(SceneArray[22]));
	
		}
		break;
		case SCENE_INFO_KEY_ID24:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID24);
			cdn_nvm3_write(SCENE_INFO_KEY_ID24,(uint8_t *)(&SceneArray[23]),sizeof(SceneArray[23]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 24] len = %d \r\n",sizeof(SceneArray[23]));
	
		}
		break;
		case SCENE_INFO_KEY_ID25:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID25);
			cdn_nvm3_write(SCENE_INFO_KEY_ID25,(uint8_t *)(&SceneArray[24]),sizeof(SceneArray[24]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 25] len = %d \r\n",sizeof(SceneArray[24]));
	
		}
		break;
		case SCENE_INFO_KEY_ID26:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID26);
			cdn_nvm3_write(SCENE_INFO_KEY_ID26,(uint8_t *)(&SceneArray[25]),sizeof(SceneArray[25]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 26] len = %d \r\n",sizeof(SceneArray[25]));
	
		}
		break;
		case SCENE_INFO_KEY_ID27:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID27);
			cdn_nvm3_write(SCENE_INFO_KEY_ID27,(uint8_t *)(&SceneArray[26]),sizeof(SceneArray[26]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 27] len = %d \r\n",sizeof(SceneArray[26]));
	
		}
		break;
		case SCENE_INFO_KEY_ID28:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID28);
			cdn_nvm3_write(SCENE_INFO_KEY_ID28,(uint8_t *)(&SceneArray[27]),sizeof(SceneArray[27]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 28] len = %d \r\n",sizeof(SceneArray[27]));
	
		}
		break;
		case SCENE_INFO_KEY_ID29:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID29);
			cdn_nvm3_write(SCENE_INFO_KEY_ID29,(uint8_t *)(&SceneArray[28]),sizeof(SceneArray[28]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 29] len = %d \r\n",sizeof(SceneArray[28]));
	
		}
		break;
		case SCENE_INFO_KEY_ID30:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID30);
			cdn_nvm3_write(SCENE_INFO_KEY_ID30,(uint8_t *)(&SceneArray[29]),sizeof(SceneArray[29]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 30] len = %d \r\n",sizeof(SceneArray[29]));
	
		}
		break;
		case SCENE_INFO_KEY_ID31:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID31);
			cdn_nvm3_write(SCENE_INFO_KEY_ID31,(uint8_t *)(&SceneArray[30]),sizeof(SceneArray[30]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 30] len = %d \r\n",sizeof(SceneArray[30]));
	
		}
		break;
		case SCENE_INFO_KEY_ID32:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID32);
			cdn_nvm3_write(SCENE_INFO_KEY_ID32,(uint8_t *)(&SceneArray[31]),sizeof(SceneArray[31]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 31] len = %d \r\n",sizeof(SceneArray[31]));
	
		}
		break;
		case SCENE_INFO_KEY_ID33:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID33);
			cdn_nvm3_write(SCENE_INFO_KEY_ID33,(uint8_t *)(&SceneArray[32]),sizeof(SceneArray[32]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 32] len = %d \r\n",sizeof(SceneArray[32]));
	
		}
		break;
		case SCENE_INFO_KEY_ID34:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID34);
			cdn_nvm3_write(SCENE_INFO_KEY_ID34,(uint8_t *)(&SceneArray[33]),sizeof(SceneArray[33]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 33] len = %d \r\n",sizeof(SceneArray[33]));
	
		}
		break;
		case SCENE_INFO_KEY_ID35:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID35);
			cdn_nvm3_write(SCENE_INFO_KEY_ID35,(uint8_t *)(&SceneArray[34]),sizeof(SceneArray[34]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 34] len = %d \r\n",sizeof(SceneArray[34]));
	
		}
		break;
		case SCENE_INFO_KEY_ID36:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID36);
			cdn_nvm3_write(SCENE_INFO_KEY_ID36,(uint8_t *)(&SceneArray[35]),sizeof(SceneArray[35]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 35] len = %d \r\n",sizeof(SceneArray[35]));
	
		}
		break;
		case SCENE_INFO_KEY_ID37:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID37);
			cdn_nvm3_write(SCENE_INFO_KEY_ID37,(uint8_t *)(&SceneArray[36]),sizeof(SceneArray[36]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 36] len = %d \r\n",sizeof(SceneArray[36]));
	
		}
		break;
		case SCENE_INFO_KEY_ID38:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID38);
			cdn_nvm3_write(SCENE_INFO_KEY_ID38,(uint8_t *)(&SceneArray[37]),sizeof(SceneArray[37]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 37] len = %d \r\n",sizeof(SceneArray[37]));
	
		}
		break;
		case SCENE_INFO_KEY_ID39:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID39);
			cdn_nvm3_write(SCENE_INFO_KEY_ID39,(uint8_t *)(&SceneArray[38]),sizeof(SceneArray[38]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 38] len = %d \r\n",sizeof(SceneArray[38]));
	
		}
		break;
		case SCENE_INFO_KEY_ID40:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID40);
			cdn_nvm3_write(SCENE_INFO_KEY_ID40,(uint8_t *)(&SceneArray[39]),sizeof(SceneArray[39]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 39] len = %d \r\n",sizeof(SceneArray[39]));
	
		}
		break;
		case SCENE_INFO_KEY_ID41:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID41);
			cdn_nvm3_write(SCENE_INFO_KEY_ID41,(uint8_t *)(&SceneArray[40]),sizeof(SceneArray[40]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 40] len = %d \r\n",sizeof(SceneArray[40]));
	
		}
		break;
		case SCENE_INFO_KEY_ID42:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID42);
			cdn_nvm3_write(SCENE_INFO_KEY_ID42,(uint8_t *)(&SceneArray[41]),sizeof(SceneArray[41]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 41] len = %d \r\n",sizeof(SceneArray[41]));
	
		}
		break;
		case SCENE_INFO_KEY_ID43:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID43);
			cdn_nvm3_write(SCENE_INFO_KEY_ID43,(uint8_t *)(&SceneArray[42]),sizeof(SceneArray[42]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 42] len = %d \r\n",sizeof(SceneArray[42]));
	
		}
		break;
		case SCENE_INFO_KEY_ID44:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID44);
			cdn_nvm3_write(SCENE_INFO_KEY_ID44,(uint8_t *)(&SceneArray[43]),sizeof(SceneArray[43]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 43] len = %d \r\n",sizeof(SceneArray[43]));
	
		}
		break;
		case SCENE_INFO_KEY_ID45:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID45);
			cdn_nvm3_write(SCENE_INFO_KEY_ID45,(uint8_t *)(&SceneArray[44]),sizeof(SceneArray[44]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 44] len = %d \r\n",sizeof(SceneArray[44]));
	
		}
		break;
		case SCENE_INFO_KEY_ID46:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID46);
			cdn_nvm3_write(SCENE_INFO_KEY_ID46,(uint8_t *)(&SceneArray[45]),sizeof(SceneArray[45]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 45] len = %d \r\n",sizeof(SceneArray[45]));
	
		}
		break;
		case SCENE_INFO_KEY_ID47:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID47);
			cdn_nvm3_write(SCENE_INFO_KEY_ID47,(uint8_t *)(&SceneArray[46]),sizeof(SceneArray[46]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 46] len = %d \r\n",sizeof(SceneArray[46]));
	
		}
		break;
		case SCENE_INFO_KEY_ID48:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID48);
			cdn_nvm3_write(SCENE_INFO_KEY_ID48,(uint8_t *)(&SceneArray[47]),sizeof(SceneArray[47]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 47] len = %d \r\n",sizeof(SceneArray[47]));
	
		}
		break;
		case SCENE_INFO_KEY_ID49:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID49);
			cdn_nvm3_write(SCENE_INFO_KEY_ID49,(uint8_t *)(&SceneArray[48]),sizeof(SceneArray[48]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 48] len = %d \r\n",sizeof(SceneArray[48]));
	
		}
		break;
		case SCENE_INFO_KEY_ID50:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID50);
			cdn_nvm3_write(SCENE_INFO_KEY_ID50,(uint8_t *)(&SceneArray[49]),sizeof(SceneArray[49]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 49] len = %d \r\n",sizeof(SceneArray[49]));
	
		}
		break;
		case SCENE_INFO_KEY_ID51:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID51);
			cdn_nvm3_write(SCENE_INFO_KEY_ID51,(uint8_t *)(&SceneArray[50]),sizeof(SceneArray[50]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 50] len = %d \r\n",sizeof(SceneArray[50]));
	
		}
		break;
		case SCENE_INFO_KEY_ID52:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID52);
			cdn_nvm3_write(SCENE_INFO_KEY_ID52,(uint8_t *)(&SceneArray[51]),sizeof(SceneArray[51]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 51] len = %d \r\n",sizeof(SceneArray[51]));
	
		}
		break;
		case SCENE_INFO_KEY_ID53:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID53);
			cdn_nvm3_write(SCENE_INFO_KEY_ID53,(uint8_t *)(&SceneArray[52]),sizeof(SceneArray[52]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 52] len = %d \r\n",sizeof(SceneArray[52]));
	
		}
		break;
		case SCENE_INFO_KEY_ID54:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID54);
			cdn_nvm3_write(SCENE_INFO_KEY_ID54,(uint8_t *)(&SceneArray[53]),sizeof(SceneArray[53]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 53] len = %d \r\n",sizeof(SceneArray[53]));
	
		}
		break;
		case SCENE_INFO_KEY_ID55:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID55);
			cdn_nvm3_write(SCENE_INFO_KEY_ID55,(uint8_t *)(&SceneArray[54]),sizeof(SceneArray[54]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 54] len = %d \r\n",sizeof(SceneArray[54]));
	
		}
		break;
		case SCENE_INFO_KEY_ID56:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID56);
			cdn_nvm3_write(SCENE_INFO_KEY_ID56,(uint8_t *)(&SceneArray[55]),sizeof(SceneArray[55]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 55] len = %d \r\n",sizeof(SceneArray[55]));
	
		}
		break;
		case SCENE_INFO_KEY_ID57:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID57);
			cdn_nvm3_write(SCENE_INFO_KEY_ID57,(uint8_t *)(&SceneArray[56]),sizeof(SceneArray[56]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 56] len = %d \r\n",sizeof(SceneArray[56]));
	
		}
		break;
		case SCENE_INFO_KEY_ID58:
		{
			cdn_nvm3_delete(SCENE_INFO_KEY_ID58);
			cdn_nvm3_write(SCENE_INFO_KEY_ID58,(uint8_t *)(&SceneArray[57]),sizeof(SceneArray[57]));
			SetSaveFlashFlag(0);//清除要保存的场景ID标志
			TRACE("zsm: CheckLightInfoIfChange SCENE_INFO_KEY_ID1[Scene 57] len = %d \r\n",sizeof(SceneArray[57]));
	
		}
		break;	
		default:
		break;
	}
}

//开机读取flash中灯的信息
void ReadLightInfoFromFlash(void)
{
	 cdn_nvm3_read(LIGHT_INFO_KEY_ID59,(char *)(&CdnLightNode));
	 
#if CDN_LIGHT_BULB
  cdn_nvm3_read(POWER_MODE_KEY_ID60,(char *)(&Power_SelectMode));
  TRACE("lj:Flash_Power_Mode = 0x%02x\n",Power_SelectMode.Power_Mode);
#endif

#if SCENE_POWER_OFF_MEMORY
  cdn_nvm3_read(SCENE_MEMORY_KEY_ID61,(char *)(&LightSceneMemoryID));
  TRACE("lj:LightSceneMemoryID = 0x%02x\n",LightSceneMemoryID);
#endif
	
  SetCurrentBrightness(0);
  SetCurrentCTvalue(0);
	CdnLightNode.GroupLightOnOff = 0;

	for(uint8_t i = 0;i < SCENE_CNT;i++)
	{
		cdn_nvm3_read(i + 1, (char *)(&SceneArray[i]));
	}
}
//判断组地址是否有效
uint8_t is_valid_group(uint16_t GroupAdr)
{
	if(GroupAdr < LIGHT_MIN_GROUP_NUM || GroupAdr > LIGHT_MAX_GROUP_NUM)
	{
		return ERROR;
	}
	return SUCCESS;
}
//判断场景号是否有效
uint8_t is_valid_scene(uint8_t Sceneid)
{
	if(Sceneid < SCENE_MIN || Sceneid > SCENE_CNT)
	{
		return ERROR;
	}
	return SUCCESS;
}

uint8_t is_valid_device(uint8_t Pid, uint8_t Sid)
{
  if(Pid ==  AI_LIGHT_TYPE)
  {
    if((Sid == CDN_AI_12W_BULB1) || (Sid == CDN_AI_12W_BULB2)
      || (Sid == CDN_AI_24W_BULB1) || (Sid == CDN_AI_24W_BULB2)
      || (Sid == CDN_AI_STRIP1) || (Sid == CDN_AI_STRIP2))
    return SUCCESS;
  }
    return ERROR;
}



